package com.example.hotelapp.Utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.hotelapp.R;
import com.example.utils.Utils.CustomVideoView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import static com.example.utils.CommonUtils.ClearInfo;

/**
 * 通用工具类
 */
public class MyCommonUtils {
    /**
     * 底部切换默认颜色
     */
    public static final String DefaultColor = "#bfbfbf";

    /**
     * 底部切换改变颜色
     */
    public static final String ChangeColor = "#ff3d00";

    /**
     * 博客分类
     */
    public static String TYPE = "";
    private static long exitTime = 0;

    /**
     * 播放登录视频
     */
    public static void LoginVideo(CustomVideoView videoView, String url){
        videoView.setVideoURI(Uri.parse(url));
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

            }
        });
        //循环播放
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.start();
            }
        });
        videoView.start();//开始播放
    }

    /**
     * 跳转到显示的页面
     */
    public static void IntoFragment(Fragment fragment, FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.replace(R.id.childrenshow, fragment);//初始化页面
        beginTransaction.commit();
    }

    /**
     * 有跳转的确定对话框
     */
    public static void ConfirmShow(final Context context, final Activity activity, final Class<?> clss, String text) {
        new XPopup.Builder(context).asConfirm("", text,
                new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        activity.startActivity(new Intent(context, clss));
                        ClearAllInfo(context);//清除所有的数据
                        activity.finishAffinity();
                    }
                })
                .show();
    }


    /**
     * 清除所有的SharedPreferences
     */
    public static void ClearAllInfo(Context context) {
        ClearInfo(context, "GetUid");
        ClearInfo(context, "GetPhone");
    }

    /**
     * 底部tab切换颜色变化
     */
    public static void setCurPoint(Activity activity, IconFontTextView icone, IconFontTextView ictwo, IconFontTextView icthree,
                                   String one, String two, String three){
        icone.setTextColor(Color.parseColor(one));
        ictwo.setTextColor(Color.parseColor(two));
        icthree.setTextColor(Color.parseColor(three));
    }


    /**
     * okhttp3上传图片
     * @param path  图片路径
     * @param url  图片上传请求接口
     * @return
     */
    public static Call UploadFileInfo(String path,String value, String url) {

        // 获取要上传的文件
        File mFile = new File(path);
        OkHttpClient client = new OkHttpClient.Builder().build();
        // 设置文件以及文件上传类型封装
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), mFile);
        // 文件上传的请求体封装
        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", mFile.getName(), requestBody)
                .addFormDataPart("u_id", value)
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(multipartBody)
                .build();

        Call call = client.newCall(request);

        return call;
    }

    /**
     * 过渡动画跳转页面
     */
    public static void transitionAnim(View view, Activity activity, Class<?> cls) {
        ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, activity.getString(R.string.trans));
        Intent intent = new Intent(activity, cls);
        ActivityCompat.startActivity(activity, intent, compat.toBundle());
    }

    /**
     * long类型转字符时间
     */
    public static String longToDate(long l){
        Date date = new Date(l);
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        return sd.format(date);
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String CurrentTimeShow(){
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        String time = simpleDateFormat.format(date);
        return time;
    }

    /**
     * Toast封装
     */
    public static void ToastShow(Activity activity,String text){
        Toast.makeText(activity,text,Toast.LENGTH_LONG).show();
    }

    /**
     * 检查手机号码
     */
    public static boolean checkPhoneValid(String mobiles){
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

}
